#!/usr/bin/env bash
# Copyright (c) 2021 Presto Labs Pte. Ltd.
# Author: leon

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(git rev-parse --show-toplevel)"
LOG_DIR="${REPO_ROOT}/../log"

DATETIME="$(date +%Y%m%d-%H%M%S%z)"
LOG_FILE="log_duplicate_coin2_raw.${DATETIME}"

mkdir -p "${LOG_DIR}"
cd "${REPO_ROOT}"
cd python

source "${SCRIPT_DIR}/py_bootstrap.sh"

MACHINES=(
    "feed-01.ap-northeast-1.aws.huobi"
    "feed-02.us-east-1.aws"
    "feed-02.eu-west-1.aws"
    "feed-02.ap-northeast-2.aws"
    "feed-05.ap-northeast-1.aws"
    "feed-05.cn-hongkong.aliyun"
    "feed-01.ap-southeast-1.aws"
    "feed-10.ap-northeast-1.aws"
)

if [ -n "${1}" ]; then
  TARGET_DATE="${1}"
else
  TARGET_DATE="$(date --utc --date='2 hours ago' '+%Y%m%d')"
fi

TARGET_DATE_TO_REMOVE="$(date --utc --date='60 hours ago' '+%Y%m%d')"

echo "TARGET_DATE=${TARGET_DATE}"
echo "TARGET_DATE_TO_REMOVE=${TARGET_DATE_TO_REMOVE}"

ROOT_SRC="/remote/iosg/coin-mirror-1/buckets/feed.raw.coin"
ROOT_DST="/export/iosg/coin/data/flow"

date

for machine in ${MACHINES[*]}
do
  SRC_DIR="${ROOT_SRC}/${machine}/${TARGET_DATE}/"
  DST_DIR="${ROOT_DST}/${machine}/${TARGET_DATE}/"
  DST_DIR_TO_REMOVE="${ROOT_SRC}/${machine}/${TARGET_DATE_TO_REMOVE}"
  rm -rf "${DST_DIR_TO_REMOVE}"
  echo ${SRC_DIR} ${DST_DIR}
  rsync -avu --delete ${SRC_DIR} ${DST_DIR}
done

date
